/**
 * 加载场景
 * Name = loadScence
 * DateTime = Sat Jan 15 2022 16:34:02 GMT+0800 (中国标准时间)
 * Author = Created by zhj9922
 * FileBasename = loadScence.ts
 * gitee： https://gitee.com/coyeking
 */

import { _decorator, Component, Node,ProgressBar, Label,resources,sys,director } from 'cc';
const { ccclass, property } = _decorator;
import { audioManager } from '../framework/audioManager';
import { constant } from '../define/constant';
import { httpApi } from '../api/httpApi';
import { userGlobal } from '../global/userGlobal';
 
@ccclass('loadScence')
export class loadScence extends Component {
    @property({
        type: ProgressBar,
        displayName:'进度条',
    })
    public ui_ProgressBar: ProgressBar = null;
    
    @property({
        type: Label,
        displayName:'进度值',
    })
    public ui_progress: Label = null;
	
	
    onLoad () {
        this.getSystemInfo();
    }
    onDestroy () {};

    private getSystemInfo() {
        var _this = this;
        httpApi.getSystemInfo((result)=>{
            userGlobal.instance.stSystemInfo(result.data);
            _this.loadRes();
        });
    }
	
    private loadRes(){
        var _this = this;
        _this.ui_ProgressBar.node.active = true;
        resources.loadDir('/', function (num, totalNum, item) {
            var pge = num/totalNum;
            _this.ui_ProgressBar.progress = pge;
            _this.ui_progress.string = `资源加载中...${Math.round(pge*100)}%`;
        }.bind(this),function(err, assets) {
            _this.getUserInfo();
        }.bind(this));
    };

    
    private getUserInfo(){
        if (sys.localStorage.getItem(constant.LOCAL_STORAGE.USER_TOKEN)){
            httpApi.getUserInfo((result)=>{
                userGlobal.instance.setUserInfo(result.data.userinfo);
                setTimeout(function(){
                    director.loadScene("hallscence");
                },1000); 
            });
            return;
        }
        this.toLoginscence();
    };

    private toLoginscence() {
        director.loadScene("loginscence");
    };

    /**
     * 按钮点击事件
     * @param event 
     * @param customEventData 
     */
	private onClick (event: Event, customEventData: string) {
        audioManager.instance.playSound();
        let c = `code：${Math.random()}`;

        httpApi.getSystemInfo(function(result){
            console.log('result==>',result);
        });
    }
}

